<?php
/*
 * Copyright 2008 by Oliver Radwan, Maxwell Palmer, Nolan McNair,
 * Taylor Talmage, and Allen Tucker.  This program is part of RMH Homebase.
 * RMH Homebase is free software.  It comes with absolutely no warranty.
 * You can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
*/

include_once("Shift.php");
/* A class to manage an RMHDate
 * @version May 1, 2008
 * @author Taylor and Maxwell Palmer
 */

class RMHdate {
	private $id;		  // "mm-dd-yy" form of this date: used as a key
	private $month;       // Textual month of the year  (e.g., Jan)
	private $day;         // Textual day of the week (Mon - Sun)
	private $dom;		  // Numerical day of month
	private $month_num;	  // Numberical month
	private $day_of_week; // Numerical day of the week (1-7, Mon = 1)
	private $day_of_year; // Numerical day of the year (1-366)
	private $year;        // Numerical year (e.g., 2008)
	private $shifts;      // array of Shifts
	private $chef_notes;  // text field for guest chef notes
	private $mgr_notes;	  // notes on night/weekend manager
	/*
	 * Construct an RMHdate and initialize its vacant shifts
	 * Test that arguments $mm, $dd, $yy are valid, using the
	 * function checkdate
	 */
	function __construct($id,$shifts,$chef_notes,$mgr_notes){
	  $mm=substr($id,0,2);
	  $dd=substr($id,3,2);
	  $yy=substr($id,6,2);
      if (! checkdate($mm, $dd, $yy)) {
      	 $this->id = null;
      	 echo "Error: invalid date for RMHdate constructor " . $mm.$dd.$yy;
      	 return;
      }
		$my_date = mktime(0, 0, 0, $mm, $dd, $yy);
		$this->id = date("m-d-y", $my_date);
		$this->month = date("M", $my_date);
		$this->day = date("D", $my_date);
		$this->year = date("Y", $my_date);
		$this->day_of_week = date("N", $my_date);
		$this->day_of_year = date("z", $my_date) + 1;
		$this->dom = date("d",$my_date);
		$this->month_num = date("m",$my_date);
		if ($shifts!==null)
		   $this->shifts = $shifts;
		else
		   $this->generate_shifts();
		$this->chef_notes = $chef_notes;
		$this->mgr_notes = $mgr_notes;
	}

	function generate_shifts() {
		$this->shifts = array();
		if ($this->day_of_week <= 5) {
		   $this->shifts["9-12"] = new Shift($this->id."-9-12", 2, null, "", "");
		   $this->shifts["12-15"] = new Shift($this->id."-12-15", 2, null, "", "");
		   $this->shifts["15-18"] = new Shift($this->id."-15-18", 2, null, "", "");
		   $this->shifts["9-12Fam"] = new Shift($this->id."-9-12Fam",1, null, "", "");
		   $this->shifts["12-15Fam"] = new Shift($this->id."-12-15Fam",1, null, "", "");
		   $this->shifts["15-18Fam"] = new Shift($this->id."-15-18Fam",1, null, "", "");
		   if ($this->day_of_week != 5)
		      $this->shifts["18-21"] = new Shift($this->id."-18-21", 3, null, "", "");
		   else
		      $this->shifts["night"] = new Shift($this->id."-night", 1, null, "", "");
		}
		else if ($this->day_of_week == 6) {
		   $this->shifts["10-13"] = new Shift($this->id."-10-13", 2, null, "", "");
		   $this->shifts["13-16"] = new Shift($this->id."-13-16", 2, null, "", "");
		   $this->shifts["night"] = new Shift($this->id."-night", 1, null, "", "");
	    }
		else {
		   $this->shifts["9-12"] = new Shift($this->id."-9-12", 2, null, "", "");
		   $this->shifts["14-17"] = new Shift($this->id."-14-17", 2, null, "", "");
		   $this->shifts["17-21"] = new Shift($this->id."-17-21", 2, null, "", "");
	    }
	}
	/*
	 * @return "mm-dd-yy"
	 */
	function get_id() {
		return $this->id;
	}

	function get_dom() {
		return $this->dom;
	}

	/*
	 * @return the textual day of the week
	 */
	function get_day(){
		return $this->day;
	}

	/*
	 * @return the numerical day of the week
	 */
	function get_day_of_week(){
		return $this->day_of_week;
	}

	/*
	 * @return the numerical day of the year (starting at 1)
	 */
	function get_day_of_year(){
		return $this->day_of_year;
	}

	/*
	 * @return the year
	 */
	function get_year(){
		return $this->year;
	}

	/*
	 * @return the shifts
	 */
	function get_shifts(){
		return $this->shifts;
	}
	/*
	 * @return the number of shifts for this day
	 */
	function get_num_shifts() {
		return count($this->shifts);
	}
	/*
	 * @return a shift
	 * @param the shift's key
	 */
	function get_shift($key){
		return $this->shifts[$key];
	}
	/**
	 * if there's a shift starting at $shift_start, return its id.
	 * Otherwise, return false
	 */
	function get_shift_id($shift_start) {
		for ($i=$shift_start+1; $i<22; $i++) {
			$candidate = $this->id . "-" . $shift_start . "-" . $i;
		    foreach ($this->shifts as $shift)
		       if ($shift->get_id()==$candidate)
		          return $shift->get_id();
		}
		return false;
	}
	/*
	 * replace a shift by a new shift in a date's associative array of shifts
	 * @param the shift, the new shift
	 */
	function replace_shift($shift, $newshift) {
		$newshifts = array();
		foreach ($this->shifts as $key => $value)
		  if ($this->get_shift($key)->get_id()===$shift->get_id())
		    $newshifts[$newshift->get_id()] = $newshift;
		  else 
		    $newshifts[$key] = $value;
		$this->shifts = $newshifts;
		return $this;
	}
	/**
	 * @return a string name of the date
	 */
	 function get_name() {
	 	return date("F j, Y",mktime(0,0,0,$this->month_num,$this->dom,$this->year));
	 }
	 function get_end_time() {
	 	return mktime(23,59,59,$this->month_num,$this->dom,$this->year);
	 }
	 function get_chef_notes() {
	 	return $this->chef_notes;
	 }
	 function set_chef_notes($s){
	 	$this->chef_notes=$s;
	 }
	function get_mgr_notes() {
	 	return $this->mgr_notes;
	 }
	 function set_mgr_notes($s){
	 	$this->mgr_notes=$s;
	 }
}

?>